package com.lw.kotlin_mvvm_master.view

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import com.gyf.immersionbar.ImmersionBar
import com.lw.common.Constants
import com.lw.common.customview.MyBottomDialog
import com.lw.common.utils.MMKVUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@SuppressLint("CustomSplashScreen")
class SplashActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ImmersionBar.with(this).init()
        init()
    }

    private fun init() {
        val isFirst: Boolean = MMKVUtils.getBoolean(Constants.IS_FIRST)
        if (!isFirst) {
            //如果没有同意条款继续弹窗
            val myBottomDialog = MyBottomDialog()
            myBottomDialog.show(supportFragmentManager, "1")
            myBottomDialog.setCancelable(false)
            myBottomDialog.setOnClickListener(object : MyBottomDialog.OnClickListener {
                override fun onClick() {
                    MMKVUtils.put(Constants.IS_FIRST, true)
                    goWhere()
                }
            })
        } else {
            CoroutineScope(Dispatchers.IO).launch {
                delay(1000)
                goWhere()
            }
        }
    }

    private fun goWhere() {
        val token = MMKVUtils.getString(Constants.KEY_TOKEN)
        startActivity(Intent(this, MainActivity::class.java))
        finish()
    }
}